package com.jdwx.opengl.kotlin2
import android.text.format.DateFormat.format
import java.util.*
import kotlinx.coroutines.*
import java.text.SimpleDateFormat
import java.util.concurrent.TimeUnit
import kotlin.concurrent.thread

interface Subject {
    fun hello() }
class RealSubject(val name: String) : Subject {
    override fun hello() {
        val now = Date()
        println("Hello, REAL $name! Now is $now") }
}
class ProxySubject(val sb: Subject) : Subject by sb {
    override fun hello() {
        println("Before ! Now is ${Date()}")
        sb.hello()
        println("After ! Now is ${Date()}")
    }
}
class DelegatePropertiesDemo {
    //var content: String by Content()
    override fun toString(): String {
        return "DelegatePropertiesDemo Class"
    }
}

/**
 * 定义的函数式编程
 */
fun f(x:Int) : Int{
    return 3
}
fun p(y:Int) : Int{
    return 7
}
fun fp(f: (Int)->Int, p: (Int)->Int) : (Int)->Int {
    return {x -> p(f(x))}
}
//end

//eg
fun isOdd(x: Int) = x % 2 != 0
fun length(s: String) = s.length
fun <A, B, C> compose(f: (B) -> C,
                      g: (A) -> B)
        : (A) -> C {

    return { vv -> f(g(vv)) }

}
//end
fun firstCoroutineDemo0() {

}
//fun main(args: Array<String>) {
//    val subject = RealSubject("World")
//    subject.hello()
//    println("-------------------------")
//    val proxySubject = ProxySubject(subject)
//    proxySubject.hello()
//
//    val oddLength = compose(::isOdd, ::length)
//    val strings = listOf("a", "ab", "abc")
//    println(strings.filter(oddLength)) // [a, abc]
//
//    GlobalScope.launch(CommonPool) {
//        delay(3000L, TimeUnit.MILLISECONDS)
//        println("Hello,")
//    }
//    println("World!")
//    Thread.sleep(5000L)
//
//}
suspend fun runCoroutineDemo() {
    println("runCoroutineDemo!")

    run{
        delay(1000)
        println("suspend,")
    }
    println("runCoroutineDemo!")
    Thread.sleep(5000L)
}
fun firstCoroutineDemo() = runBlocking{
    launch{
        delay(3000)
        println("[firstCoroutineDemo] Hello, 1")
    }
    println("[firstCoroutineDemo] Hello, 2")

    println("[firstCoroutineDemo] World!")
}
fun testJoinCoroutine() = runBlocking<Unit> { // Start a coroutine
     var c1 = launch{
        println("C1 Thread: ${Thread.currentThread()}")
        println("C1 Start")
        delay(3000L)
        println("C1 World! 1")
    }
    var c2 = launch{
        println("C2 Thread: ${Thread.currentThread()}")
        println("C2 Start")
        delay(5000L)
        println("C2 World! 2")
    }
    println("Main Thread: ${Thread.currentThread()}")
    println("Hello,")
    println("Hi,")
    c1.join()
    c2.join()

    println("Main Thread: ${c1.isActive},${c1.isCompleted}")
    println("Main Thread: ${c2.isActive},${c2.isCompleted}")

}
fun main(args: Array<String>) = runBlocking{
    val time = SimpleDateFormat("hh:MM:ss")

//    GlobalScope.launch {
//        delay(1000)
//        println("${time.format(Date())}  World!")
//    }
//
//    println("${time.format(Date())}  Hello, ")
//    Thread.sleep(2000)
//    runBlocking {     // 但是这个表达式阻塞了主线程
//        delay(2000L)  // ……我们延迟 2 秒来保证 JVM 的存活
//    }
    //挂起函数
//    GlobalScope.launch {
//        runCoroutineDemo()
//    }
//
//
//    println("$time: T0")
//
//    launch {
//        println("1: T1")
//        delay(3000L)
//        println("2: T2 Hello,")
//    }
//    println("3")
//    delay(5000L)
//    println( "4" )
    firstCoroutineDemo()
    testJoinCoroutine()
}